<?php
declare(strict_types=1);

namespace lib\data\traits;

trait UnikTrait {
    static array $pk = [];

    static array $unik_cache = [];

    protected function traitInit(){
        static::$pk[$this->id] = $this;
        static::$unik_cache[$this->unik] = $this;
    }

    public static function getByUnik(string $unik) :?static{
        return static::$unik_cache[$unik]??null;
    }

    public static function get(int $id) :?static {
        return static::$pk[$id]??null;
    }

    public function remove(){
        unset(static::$pk[$this->id]);
        unset(static::$unik_cache[$this->unik]);
    }
}